package View;

import Config.GlobalVariables;
import DAO.ProductsDAO;
import Entity.Robot;
import Entity.RobotStatus;
import Service.SupervisorService;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Warehouse3 extends JFrame {
    private final Image robotAIcon = scaleImage("src\\View\\images\\a-orange.png", 24, 24);
    private SupervisorService supervisorService;
    private List<Robot> robots; // 机器人列表

    public Warehouse3(SupervisorService supervisorService) {
        this.supervisorService = supervisorService;
        this.robots = supervisorService.getAllRobots();  // 获取所有机器人列表

        setTitle("Robot Animation View");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(GlobalVariables.GRID_COLUMNS * GlobalVariables.CELL_SIZE + 200,
                GlobalVariables.GRID_ROWS * GlobalVariables.CELL_SIZE + 200);
        setLocationRelativeTo(null);

        add(new GridPanel());

        // 动画定时器，更新每100毫秒
        Timer timer = new Timer(10, e -> {
            for (Robot robot : robots) {
                robot.move();  // 更新机器人位置
            }
            repaint();  // 刷新界面
        });
        timer.start();
    }

    private class GridPanel extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 绘制网格和内容
            for (int row = 0; row < GlobalVariables.GRID_ROWS; row++) {
                for (int col = 0; col < GlobalVariables.GRID_COLUMNS; col++) {
                    int x = col * GlobalVariables.CELL_SIZE + 100;
                    int y = row * GlobalVariables.CELL_SIZE + 100;

                    // 绘制单元格边框
                    g2d.setColor(Color.LIGHT_GRAY);
                    g2d.drawRect(x, y, GlobalVariables.CELL_SIZE, GlobalVariables.CELL_SIZE);

                    // 根据 gridList 的编号绘制内容
                    int cellValue = GlobalVariables.gridList.get(row).get(col);
                    if (cellValue == 1) {
                        // 这里的代码根据需要调整以适应新的图标或功能
                    }
                }
            }

            // 绘制所有机器人
            for (Robot robot : robots) {
                double robotX = robot.getY() * GlobalVariables.CELL_SIZE + 100;
                double robotY = robot.getX() * GlobalVariables.CELL_SIZE + 100;
                g2d.drawImage(robotAIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                        (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
            }
        }
    }

    // 工具方法：调整图像大小
    private Image scaleImage(String imagePath, int width, int height) {
        ImageIcon icon = new ImageIcon(imagePath);
        return icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
    }

    public static void main(String[] args) {
        // 假设你已经有了 SupervisorService 的实例
        SupervisorService supervisorService = new SupervisorService(new ArrayList<>());

        // 启动新的视图窗口
        SwingUtilities.invokeLater(() -> {
            Warehouse3 frame = new Warehouse3(supervisorService);
            frame.setVisible(true);
        });
    }
}
